package com.centriole.service.workflow.service.impl;

import com.centriole.service.workflow.common.Constant;
import com.centriole.service.workflow.dao.WorkPageMapper;
import com.centriole.service.workflow.entity.WorkPage;
import com.centriole.service.workflow.enums.StateType;
import com.centriole.service.workflow.service.WorkPageService;
import com.centriole.service.workflow.util.CommonUtil;
import com.centriole.service.workflow.util.UserUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**操作页面service实现类
 * @author 吴凯
 *
 */
@Service
public class WorkPageServiceImpl implements WorkPageService {
	@Resource
	private WorkPageMapper workPageMapper;

	@Override
	public List<WorkPage> selectByProcessIdAndType(String processId, String type) {
		List<WorkPage> list = workPageMapper.selectByProcessIdAndType(processId, type);
		return list;
	}

	@Override
	public void insertOrEdit(WorkPage workPage) throws Exception {

		if(CommonUtil.isEmpty(workPage.getId())){
			workPage.setId(CommonUtil.getUuid());
			workPage.setState(StateType.NORMAL.statusCode());
			workPage.setCreateDate(new Date());
			workPage.setCreatorId(StringUtils.isBlank( workPage.getCreatorId() )?"default":workPage.getCreatorId());
			workPage.setModifierId( StringUtils.isBlank( workPage.getModifierId() )?"default":workPage.getCreatorId()  );
			workPage.setPageHeight(StringUtils.isBlank( workPage.getPageHeight() )?Constant.pageHeight:workPage.getPageHeight());
			workPage.setPageWidth(StringUtils.isBlank(workPage.getPageWidth())?Constant.pageWidth:workPage.getPageWidth());
			workPageMapper.insert(workPage);
		}else{
			check(workPage);
			String modifierId = workPage.getModifierId();
			modifierId = StringUtils.isBlank(modifierId)?UserUtils.currentUserInfo().getEmpId()+"":modifierId;
			workPage.setModifierId(modifierId);
			workPageMapper.update(workPage);
		}
	}

	/**字段检查
	 * @param workPage
	 * @throws Exception
	 */
	private void check(WorkPage workPage) throws Exception {
		if(CommonUtil.isEmpty(workPage.getProcessId())){
			throw new Exception("流程不能为空");
		}
		if(CommonUtil.isEmpty(workPage.getPageName())){
			throw new Exception("页面名称不能为空");
		}else{
			System.out.println(workPage.getPageName());
			List<WorkPage> list = workPageMapper.selectByPidAndPageName(workPage.getProcessId(), workPage.getPageName());
			if(CommonUtil.isNotEmpty(list) &&
					!(CommonUtil.isNotEmpty(workPage.getId()) && list.get(0).getId().equals(workPage.getId()))){
				throw new Exception("页面名称已存在");
			}
		}
		if(CommonUtil.isEmpty(workPage.getPageUrl())){
			throw new Exception("页面地址不能为空");
		}
		if(CommonUtil.isEmpty(workPage.getPageWidth())){
			throw new Exception("页面宽度不能为空");
		}
		if(CommonUtil.isEmpty(workPage.getPageHeight())){
			throw new Exception("页面高度不能为空");
		}
	}

	@Override
	public void logicDelete(String id) {
		workPageMapper.logicDelete(id);
	}

	@Override
	public List<WorkPage> select() {
		List<WorkPage> list = workPageMapper.select();
		return list;
	}
	
	@Override
	public WorkPage selectById(String id) {
		WorkPage workPage = workPageMapper.selectById(id);
		return workPage;
	}
}
